﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Way.Page;


namespace Acc.Manage.Report_SF_aspx
{
    public partial class SaleList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!Page.IsPostBack)
            {
                if (Request.QueryString["ID"] != string.Empty && Request.QueryString["p"] == "c")
                {
                    DataTable dt = BindGV();
                    this.lblCountMoney.Visible = true;
                    this.lblCountMoney1.Visible = true;
                    this.lblBuhanshui.Visible = true;
                    this.lblBuhanshui1.Visible = true;
                    this.GridView1.DataSource = dt;
                    this.GridView1.DataBind();

                }
                if (Request.QueryString["ID"] != string.Empty && Request.QueryString["p"] == "p")
                {
                    DataTable dt = BindGV();
                    this.lblCountMoney.Visible = false;
                    this.lblCountMoney1.Visible = false;
                    this.lblBuhanshui.Visible = false;
                    this.lblBuhanshui1.Visible = false;
                    this.GridView2.DataSource = dt;
                    this.GridView2.DataBind();

                }



            }
        }

        public double numCount = 0;
        public double moneyCount = 0;
        public double buhanshui = 0;
        public string code = "";
        public string CUSTOMERNAME = "";
        public string createman = "";
        public string Reviewedman = "";
        private DataTable BindGV()
        {
            string xsid = Request.QueryString["ID"].ToString();
            //DataTable Sourcedt = SqlHelper.ExeAll("select so.SourceCode,ms.code,ms.fname as materialName ,ms.NETWEIGHT as specification,unt.UNITNAME as unit,CAST(sm.num as decimal(20,2)) num ,CONVERT(decimal(20, 2), ISNULL(sm.price, 0)) price, CONVERT(decimal(20, 2),(CAST(sm.num as decimal(20,2)) * CONVERT(decimal(20, 2),sm.price))) as countnum, '13%' as shuilv,Convert(decimal(20,2),(CAST(sm.num as decimal(20,2)) *  CONVERT(decimal(20, 2),sm.price)) / (1+0.13)) as buhanshui ,su.CUSTOMERNAME,su.TaxpayerIdentificationNumber from Acc_Bus_BusinessCommodity ms inner join Acc_WMS_StockOutOrderMaterials sm on sm.MATERIALCODE=ms.id inner join Acc_WMS_StockOutOrder so on sm.PARENTID= so.ID left join Acc_Bus_BusinessCustomer su on su.ID=so.clientNo  left join Acc_Bus_Unit unt on ms.FUNITID=unt.ID  where so.id='" + xsid + "'");
            DataTable Sourcedt = SqlHelper.ExeAll("select so.SourceCode,ms.code,ms.fname as materialName ,ms.NETWEIGHT as specification,unt.UNITNAME as unit,CAST(sm.num as decimal(20,2)) num ,CONVERT(decimal(20, 2), ISNULL(sm.price, 0)) price, CONVERT(decimal(20, 2),(CAST(sm.num as decimal(20,2)) * CONVERT(decimal(20, 2),sm.price))) as countnum, '13%' as shuilv,Convert(decimal(20,2),CONVERT(decimal(20, 2),(CAST(sm.num as decimal(20,2)) * CONVERT(decimal(20, 2),sm.price)))  / (1+0.13)) as buhanshui ,su.CUSTOMERNAME,su.TaxpayerIdentificationNumber from Acc_Bus_BusinessCommodity ms inner join Acc_WMS_OutOrderMaterials sm on sm.MATERIALCODE=ms.id inner join Acc_WMS_OutOrder so on sm.PARENTID= so.ID left join Acc_Bus_BusinessCustomer su on su.ID=so.clientNo  left join Acc_Bus_Unit unt on ms.FUNITID=unt.ID  where so.id='" + xsid + "'");
            DataTable zbdt = SqlHelper.ExeAll(" select * from Acc_WMS_OutOrder as so where so.id='" + xsid + "'");
            //DataTable isEnable = WMSqlExcuter.ExecuteDataTable("select state from orderCommon where orderNo='" + orderNo + "' and (state='08' or state='10') ");
            if (Sourcedt.Rows.Count <= 0)
            {
                throw new Exception("无需要打印的数据！");

                //Response.Redirect("Login.aspx");
            }
            //DataTable dt = WMSqlExcuter.ExecuteDataTable("select ms.materialName ,ms.specification,ms.unit,CAST(sm.num as integer) num ,CONVERT(decimal(20, 2), ISNULL(om.price, 0)) price, CAST(sm.num as integer) * CONVERT(decimal(20, 2),om.price) as countnum, '13%' as shuilv,Convert(decimal(20,2),(CAST(sm.num as integer) *  CONVERT(decimal(20, 2),om.price)) / (1+0.13)) as buhanshui ,su.sup_name,su.dutyNo from materials ms inner join stockOutOrder_materials sm on sm.materialCode=ms.materialNo inner join stockOutOrder so on sm.orderNo= so.orderNo left join supplier su on su.sup_code=so.clientNo inner join stockOutOrder_materials om on om.orderNo=so.orderNo where sm.orderNo='" + orderNo + "'");
            foreach (DataRow dr in Sourcedt.Rows)
            {

                numCount += Convert.ToDouble(dr["num"].ToString());
                moneyCount += Convert.ToDouble(dr["countnum"].ToString());
                buhanshui += Convert.ToDouble(dr["buhanshui"].ToString());
            }
            //this.lblOrderNo.Text = orderNo;
            //this.Label3.Text = numCount;
            //this.lblCountMoney1.Text = moneyCount;
            //this.lblBuhanshui1.Text = buhanshui;
            code = zbdt.Rows[0]["Code"].ToString();
            CUSTOMERNAME = Sourcedt.Rows[0]["CUSTOMERNAME"].ToString();
            DataTable dt = SqlHelper.ExeAll("select WorkName from Acc_Bus_OfficeWorker where ID=" + zbdt.Rows[0]["Createdby"].ToString());
            if (dt.Rows.Count >= 1)
                createman = dt.Rows[0]["WorkName"].ToString();
            dt = SqlHelper.ExeAll("select WorkName from Acc_Bus_OfficeWorker where ID=" + zbdt.Rows[0]["Reviewedby"].ToString());
            if (dt.Rows.Count >= 1)
                Reviewedman = dt.Rows[0]["WorkName"].ToString();
            this.lblSNum.Text = Sourcedt.Rows[0]["TaxpayerIdentificationNumber"].ToString();
            this.lblDate.Text = Convert.ToDateTime(zbdt.Rows[0]["INOUTTIME"].ToString()).ToString("yyyy-MM-dd");// DateTime.Now.ToString("yyyy-MM-dd");
            this.lblPrintDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            return Sourcedt;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string xsid = Request.QueryString["ID"].ToString();
                string FpType = this.DropDownList1.Text;
                int dd = SqlHelper.ExeCUD("update Acc_WMS_OutOrder set FpType='" + FpType + "' where ID='" + xsid + "'");
            }
            catch (Exception)
            {

                throw;
            }


        }
    }
}